<?php
/**
 * americantaglistcontroller.php
 * Create on 2016-03-29 16:18:49
 * Create by yuebin
 */
namespace haibao\cms\business\control\pc\channel\american;
use \haibao\cms\model\filter\Article as ArticleFilter;
use \haibao\cms\model\filter\ArticleTag as ArticleTagFilter;
use \haibao\cms\model\filter\americandrama\AmericanDefine as AmericanFilter;
use \haibao\cms\model\filter\americandrama\AmericanTag as AmericanTagFilter;
use \haibao\cms\model\filter\americandrama\AmericanCategory as AmericanCategoryFilter;
use haibao\frame\data\query\Condition;

class AmericanTagListController extends \haibao\cms\business\control\AmericanController{
	
	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	/**
	 * 显示模版内容（模版动态数据的绑定和模版输出）
	 * @param : String $content
	 * @param : Array $controlData;
	 */
	public function displayTemplateHtml(){
		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$currentPage = $this->getParameter(self::$CURRENTPAGE);
		$pageable = $this->getParameter(self::$PAGEABLE);
		$siteTemplateId = $this->getParameter(self::$SITE_TEMPLATE_ID);
		$this->replaceAdPosition($controlData['auto']);
		
		$tagInfo = $this->getParameter(self::$TAG_INFO);

		$this->conditionTagId = $tagInfo->Id;
		$this->topTagId = $tagInfo->TopId;
		
		$topTagList = self::getTopTags();
		$topTagName = $topTagList[$this->topTagId]['name'];
		$topTagUrl = $topTagList[$this->topTagId]['tagchannelurl'];
		$logo = $topTagList[$this->topTagId]['logo'];
		
		if($controlData['manual']){
			$dataCondition = $controlData['manual'];
			$dataCondition['currentPage'] = $currentPage;

			$this->packageUrl();
			$this->getArticleNewList($dataCondition);
			 //$this->getArticleHotList($dataCondition);
			$this->getImageCollList($dataCondition);
			$this->getHotAmerican();
			
			$this->assign('siteTemplateId', $siteTemplateId);
			$matchDesc = $tagInfo->Desc;
			
			if(!preg_match('/\<p\>([^\s+].*?)\<\/p\>/',$tagInfo->Desc,$matchs)){
				if(!empty($matchs)){
					$matchDesc = '';
				}
			}
			$this->assign('tagName', $tagInfo->TagName);
			$this->assign('desc', $tagInfo->Desc);
			$this->assign('matchDesc', $matchDesc);

			$this->assign('topTagName', $topTagName);
			$this->assign('topTagUrl', $topTagUrl);
			$this->assign('logo', $logo);
			
			$path = $this->generateHtmlPath(__CLASS__,$this->conditionTagId.$currentPage);
			$this->generateHtml($path,$content);
			$content = $this->getHtml($path);
			$content = $this->replaceCDNLink($content);
			
			return $content;
		}
	}
	/**
	 * 获取最新资讯列表
	 * @param : Array $queryCondition 查询条件
	 */
	private function getArticleNewList($dataCondition){
		$dataCondition['source'] = __CLASS__.$this->conditionTagId;
		$dataCondition['level'] = 1;
		$dataCondition['isProImage'] = true;
		$dataCondition['isTag'] = true;
		$dataCondition['isReturnTag'] = true;
		$dataCondition['isParentTag'] = false;
		$dataCondition['isImageColl'] = false;
		$dataCondition['isImageColl'] = false;
		$dataCondition['isReturnPrimary'] = false;
		$proLevel = \haibao\cms\model\data\ArticleProImage::Level_FIVE;
		$dataCondition['proImageLevel'] = array($proLevel);
		list($articles,$proImage,$tagInfo,$articleTag,$pager) = $this->getArticleControl(5,$dataCondition);
		//print_r($articleTag);
		$finalArr = $articleIds = array();
		foreach($articles as $key=>$article){
			$temp = array();
			$articleIds[] = $article->Id;
			$temp['Id'] = $article->Id;
			$temp['Title'] = $article->Title;
			$temp['SiteType'] = $article->SiteType;
			$temp['Channel'] = self::getArticleUrl($article->PublishId,$article->ArticleTag->ParentId);
			$temp['Date'] = $this->formatDate($article->PublishTime->format('Y-m-d H:i:s'));
			$temp['EnMonth'] = strtoupper($article->PublishTime->format('M'));
			
			if(isset($articleTag[$article->Id])){
				foreach($articleTag[$article->Id] as $m=>$primary){
					if($primary['IsPrimaiy'] == 1){
					$temp['TagName'] = $tagInfo[$primary['TagId']]['TagName'];
					$temp['TagUrl'] = $tagInfo[$primary['TagId']]['Url'];
					}
				}
			}else{
				$temp['TagUrl'] = $temp['TagName'] = '';
			}
	
	
	
			if(isset($proImage[$article->Id][$proLevel])){
				$temp['ProImage'] = $proImage[$article->Id][$proLevel];
			}else{
				if(isset($proImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
					$temp['ProImage'] = $proImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
				}else{
					$temp['ProImage'] = \haibao\cms\business\control\PublicController::getArticleFirstImage($article->Id);
				}
			}
			$temp['isVideo'] = false;
			if(isset($articleTag[$article->Id])){
				$tempTagArr = array_reduce($articleTag[$article->Id], create_function('$v,$w', '$v[$w["TagId"]]=$w["TagId"];return $v;'));
				if(array_intersect($tempTagArr,self::$videoArticleTag)){
					$temp['isVideo'] = true;
				}
			}
			
			array_push($finalArr,$temp);
	
		}
		$this->assign('pageCount',$pager->getPageCount());
		$this->assign('pager', $pager);
		$this->assign('tagList',$finalArr);
	}
	
	/**
	 * 获取最热门资讯列表
	 * @param : Array $queryCondition 查询条件
	 */
	private function getArticleHotList($dataCondition){
	
		$dataCondition['source'] = __CLASS__.$this->conditionTagId;
		$dataCondition['level'] = 1;
		$dataCondition['isProImage'] = true;
		$dataCondition['isTag'] = true;
		$dataCondition['isReturnTag'] = true;
		$dataCondition['isParentTag'] = false;
		$dataCondition['isImageColl'] = false;
		$dataCondition['isDate'] = true;
		$dataCondition['dateType'] = \haibao\cms\business\control\ArticleController::DATE_TYPE_MONTH;
	
		$proImageLevel = \haibao\cms\model\data\ArticleProImage::Level_FOUR;
		$dataCondition['proImageLevel'] = array($proImageLevel);
	
		list($articles,$proImage,$tagInfo,$articleTag) = $this->getArticleControl(2,$dataCondition);
		$hotList = array();
		foreach($articles as $key=>$article){
	
			$temp = array();
			$temp['Id'] = $article->Id;
			$temp['Title'] = $article->Title;
			$temp['SiteType'] = $article->SiteType;
			$temp['Channel'] = self::getArticleUrl($article->PublishId,$articleTag[$article->Id]['ParentId']);
			$temp['Date'] = $this->formatDate($article->PublishTime->format('Y-m-d H:i:s'));
	
			if(isset($tagInfo[$articleTag[$article->Id]['TagId']])){
	
				$temp['TagName'] = $tagInfo[$articleTag[$article->Id]['TagId']]['TagName'];
				$temp['TagUrl'] = $tagInfo[$articleTag[$article->Id]['TagId']]['Url'];
			}else{
				$temp['TagUrl'] = $temp['TagName'] = '';
			}
	
			if(isset($proImage[$article->Id][$proImageLevel])){
				$temp['ProImage'] = $proImage[$article->Id][$proImageLevel];
			}else{
				if(isset($proImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
					$temp['ProImage'] = $proImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
				}else{
					$temp['ProImage'] = '';
				}
			}
	
			array_push($hotList,$temp);
	
		}
		$this->assign('hotList',$hotList);
	}
	
	/**
	 * 调取该子栏目下，最新发布的前3个图集
	 */
	public function getImageCollList($dataCondition){
	
		$dataCondition['source'] = __CLASS__.$this->conditionTagId;
		$dataCondition['level'] = 1;
		$dataCondition['limit'] = 3;
		$dataCondition['isProImage'] = true;
		$dataCondition['isTag'] = true;
		$dataCondition['isParentTag'] = false;
		//$dataCondition['isDate'] = false;
		//$dataCondition['dateType'] = \haibao\cms\business\control\ArticleController::DATE_TYPE_WEEK;
		$dataCondition['proImageLevel'] = array(\haibao\cms\model\data\ArticleProImage::Level_FIVE);
		$dataCondition['orderByNew'] = true;
		list($imageColl,$proImage) = $this->getArticleControl(4,$dataCondition);
		$imageCollList = array();
		foreach($imageColl as $key=>$coll){
	
			$temp = array();
			$temp['Id'] = $coll->Id;
			$temp['Title'] = $coll->ShortTitle;
			$temp['SiteType'] = $coll->SiteType;
			$temp['Channel'] = self::getArticleUrl($coll->Id,$coll->ImageCollectiveTag->ParentId,2);
	
	
			if(isset($proImage[$coll->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE])){
				$temp['ProImage'] = $proImage[$coll->Id][\haibao\cms\model\data\ArticleProImage::Level_FIVE];
			}else{
				if(isset($proImage[$coll->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
					$temp['ProImage'] = $proImage[$coll->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
				}else{
					$temp['ProImage'] = '';
				}
			}
			array_push($imageCollList,$temp);
		}
		$this->assign('imageCollMore',\haibao\classlibrary\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_PIC).'/piclists/'.$this->conditionTagId.'/');
		$this->assign('imageCollList', $imageCollList);
	}
	
	/**
	 * 封装发布的地址url
	 */
	private function packageUrl(){
		$topIds = self::getTopTags();
		$url = $topIds[$this->topTagId]['channelurl'].'/tv/'.$this->conditionTagId.'/';
		$this->assign('pageUrl',$url);
		$this->assign('pageSize',8);
		$this->assign('viewName','publish');
	}
	
	/**
	 * 热剧推荐
	 * 最近7天内有最新动态资讯或图集更新；
	 * 必须是剧库详情页中所有版块内容齐全，没有缺漏隐藏项；
	 */
	private function getHotAmerican(){
		//\haibao\cms\cache\American::delCache(\haibao\cms\cache\American::HOT_AMERICAN_FOR_TAG_LIST);
		$americanArr = \haibao\cms\cache\American::getHotAmericanForTagList();
		if(!$americanArr){
			$americanIdArr  = array();
			$articleFilter = new ArticleFilter();
			$articleFilter->select(
			    array(
			        ArticleFilter::CONDITION_FIELD_NAME_ID,
			        ArticleFilter::CONDITION_FIELD_NAME_TITLE,
			        ArticleFilter::CONDITION_FIELD_NAME_SHORTTITLE,
			        ArticleFilter::CONDITION_FIELD_NAME_PUBLISHTIME,
			        ArticleFilter::CONDITION_FIELD_NAME_ARTICLE_PUBLISHID
			    )
			)->limit(0,20);
			$articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_EQUAL,\haibao\cms\model\data\Article::STATUS_ENABLE);
			$articleFilter->where(ArticleFilter::CONDITION_FIELD_NAME_PUBLISHED,Condition::CONDITION_EQUAL,\haibao\cms\model\data\Article::PUBLISHED_YES);
			
			$articleTagFilter = new ArticleTagFilter();
			$articleTagFilter->select(
			    array(
			        ArticleTagFilter::CONDITION_FIELD_NAME_ARTICLE_ID,
			        ArticleTagFilter::CONDITION_FIELD_NAME_TAG_ID,
			        ArticleTagFilter::CONDITION_FIELD_NAME_PARENT_ID,
			        ArticleTagFilter::CONDITION_FIELD_NAME_IS_PRIMAIY,
			    )
			);
			$articleTagFilter->where(ArticleTagFilter::CONDITION_FIELD_NAME_PARENT_ID,Condition::CONDITION_EQUAL,self::AMERICAN_TOP_TAG_ID);
			$articleFilter->leftJoin($articleTagFilter,null,ArticleFilter::CONDITION_FIELD_NAME_ID,ArticleTagFilter::CONDITION_FIELD_NAME_ARTICLE_ID,ArticleFilter::CONDITION_FIELD_NAME_ARTICLE_TAG);
			$articleFilter->order(ArticleFilter::CONDITION_FIELD_NAME_PUBLISHTIME,false);
			$lists = \haibao\cms\business\Article::getArticleList($articleFilter);
            foreach($lists as $key=>$val){
                foreach($val->ArticleTag as $k=>$v){
                    $articleTagArr[$val->Id][] = $v->TagId;
                    $articleTagIdArr[] = $v->TagId;
                }
            }
            $americanIdArr = \haibao\cms\business\americandrama\AmericanDefine::getAmericanIdByTagId($articleTagIdArr);

			if($americanIdArr){
				$randAmericanIdArr = $americanArr = array();
				$americanIdArr = array_flip(array_flip($americanIdArr));
				$randArr = array_rand($americanIdArr,2);
				array_push($randAmericanIdArr,$americanIdArr[$randArr[0]]);
				array_push($randAmericanIdArr,$americanIdArr[$randArr[1]]);
				
				$americanFilter = new AmericanFilter();
				$americanCategoryFilter = new AmericanCategoryFilter();
				$americanFilter->select(array(
						AmericanFilter::CONDITION_FIELD_NAME_ID,
						AmericanFilter::CONDITION_FIELD_NAME_NAMECN,
						AmericanFilter::CONDITION_FIELD_NAME_NAMEEN,
						AmericanFilter::CONDITION_FIELD_NAME_AVATARURL,
				));
				$americanCategoryFilter->select(array(
						AmericanCategoryFilter::CONDITION_FIELD_NAME_CATEGORYID
				));
				$americanFilter->where(AmericanFilter::CONDITION_FIELD_NAME_ID,Condition::CONDITION_IN,$randAmericanIdArr);
				$americanFilter->innerJoin($americanCategoryFilter,null,AmericanFilter::CONDITION_FIELD_NAME_ID,AmericanCategoryFilter::CONDITION_FIELD_NAME_AMERICANID,AmericanFilter::CONDITION_FIELD_NAME_AMERICANCATEGORY);
				$americanList = \haibao\cms\business\americandrama\AmericanDefine::getAmericanDramaLists($americanFilter);
				foreach($americanList as $american){
					
					$temp = array();
					$temp['Id'] = $american->Id;
					$temp['NameCH'] = $american->NameCH;
					$temp['NameEN'] = $american->NameEN;
					$temp['AvatarUrl'] = $american->AvatarUrl;
					$temp['Category'] = array();
					foreach($american->AmericanCategory as $category){
						$cateTemp = array();
						$cateTemp['Name'] = \haibao\cms\business\americandrama\AmericanDefine::$meiju_jmtype_arr[$category->CategoryId];
						$pcUrl = $this->getAmericanSearchUrl($category->CategoryId);
						$cateTemp['Url'] = $pcUrl['PC'];
						array_push($temp['Category'],$cateTemp);
					}
					$temp['Url'] = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_TV_HOST).'meiju/'.$american->Id.'/';
					array_push($americanArr,$temp);
				}
				\haibao\cms\cache\American::setHotAmericanForTagList(json_encode($americanArr),60*60*24*7);
			}
		}
		$this->assign('hotAmericanList',$americanArr);
		$this->assign('tvLibrary',\haibao\classlibrary\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_TV_HOST).'library');
	}
}